package com.lcj.dr.controller;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.PageInfo;
import com.lcj.common.util.Result;
import com.lcj.dr.controller.form.*;
import com.lcj.dr.service.DriverService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author : lcj
 * @create 2022/11/3 21:32
 */
@RestController
@RequestMapping("/driver")
@Tag(name = "DriverController", description = "司机模块Web接口")
public class DriverController {

    @Resource
    private DriverService driverService;

    @PostMapping("/searchDriverOpenId")
    @Operation(summary = "查询司机的OpenId")
    public Result searchDriverOpenId(@RequestBody @Valid DriverBaseInfoForm form) {
        String openId = driverService.searchDriverOpenId(form.getDriverId());
        return Result.ok().put("result", openId);
    }

    @PostMapping("/registerNewDriver")
    @Operation(summary = "新司机注册")
    public Result registerNewDriver(@RequestBody @Valid RegisterNewDriverForm form) {
        //实体对象转为Map
        Map param = BeanUtil.beanToMap(form);
        Result res = driverService.registerNewDriver(param);
        return res;
    }

    @PostMapping("/updateDriverAuth")
    @Operation(summary = "更新实名认证信息")
    public Result updateDriverAuth(@RequestBody @Valid UpdateDriverAuthForm form) {
        Map param = BeanUtil.beanToMap(form);
        int rows = driverService.updateDriverAuth(param);
        return Result.ok().put("rows", rows);
    }

    @PostMapping("/createDriverFaceModel")
    @Operation(summary = "创建司机人脸模型归档")
    public Result createDriverFaceModel(@RequestBody @Valid CreateDriverFaceModelForm form) {
        String res = driverService.createDriverFaceModel(form.getDriverId(), form.getPhoto());
        return Result.ok().put("result", res);
    }

    @PostMapping("/vertificateDriverFace")
    @Operation(summary = "验证司机人脸是否正确")
    public Result vertificateDriverFace(@RequestBody @Valid CreateDriverFaceModelForm form) {
        Result res = driverService.vertificateDriverFace(form.getDriverId(), form.getPhoto());
        return res;
    }

    @PostMapping("/login")
    @Operation(summary = "登录")
    public Result login(@RequestBody @Valid LoginForm form) {
        HashMap map = driverService.login(form.getCode());
        return Result.ok().put("result",map);
    }

    @PostMapping("/searchDriverBaseInfo")
    @Operation(summary = "查询司机基本信息")
    public Result logout(@RequestBody @Valid DriverBaseInfoForm form){
        HashMap result = driverService.searchDriverBaseInfo(form.getDriverId());
        return Result.ok().put("result",result);
    }

    @PostMapping("/searchDriverByPage")
    @Operation(summary = "查询司机分页记录")
    public Result searchDriverByPage(@RequestBody @Valid SearchDriverByPageForm form) {
        PageInfo<HashMap> pageInfo = driverService.searchDriverByPage(form);
        return Result.ok().put("result", pageInfo);
    }

    @PostMapping("/searchDriverAuth")
    @Operation(summary = "查询司机认证信息")
    public Result searchDriverAuth(@RequestBody @Valid DriverBaseInfoForm form){
        HashMap result = driverService.searchDriverAuth(form.getDriverId());
        return Result.ok().put("result", result);
    }

    @PostMapping("/updatePhone")
    @Operation(summary = "更新司机电话")
    public Result updateDriverPhone(@RequestBody @Valid UpdatePhoneForm form){
        Integer rows = driverService.updateDriverPhone(form);
        if(rows > 0) {
            return Result.ok().put("rows", rows);
        } else if(rows == -2) {
            return Result.error("此手机号就是当前绑定手机号！");
        } else {
            return Result.error("修改失败，该手机号已被其他账号绑定！");
        }

    }

    @PostMapping("/searchDriverRealSummary")
    @Operation(summary = "查询司机实名信息摘要")
    public Result searchDriverDealSummary(@RequestBody @Valid DriverBaseInfoForm form){
        HashMap result = driverService.searchDriverRealSummary(form.getDriverId());
        return Result.ok().put("result", result);
    }

    @GetMapping("/searchDriverIsRealAuth/{driverId}")
    @Operation(summary = "查询司机实名认证状态")
    public Result searchDriverIsRealAuth(@PathVariable Long driverId){
        if(driverId != null) {
            HashMap map = driverService.searchDriverIsRealAuth(driverId);
            return Result.ok().put("result",map);
        } else {
            return Result.error("driverId不能为空");
        }
    }

    @PostMapping("/updateDriverRealAuth")
    @Operation(summary = "更新司机实名认证状态")
    public Result updateDriverRealAuth(@RequestBody @Valid UpdateDriverRealAuthForm form) {
        Map param = BeanUtil.beanToMap(form);
        int rows = driverService.updateDriverRealAuth(param);
        return Result.ok().put("rows", rows);
    }

    @PostMapping("/searchDriverBriefInfo")
    @Operation(summary = "查询司机简明信息")
    public Result searchDriverBriefInfo(@RequestBody @Valid DriverBaseInfoForm form) {
        HashMap map = driverService.searchDriverBriefInfo(form.getDriverId());
        return Result.ok().put("result", map);
    }

    @PostMapping("/searchCanStartWork")
    @Operation(summary = "查询司机能否开始工作")
    public Result searchCanStartWork(@RequestBody @Valid SearchCanStartWorkForm form) {
        Map param = BeanUtil.beanToMap(form);
        HashMap map = driverService.searchCanStartWork(param);
        return Result.ok().put("result", map);
    }

    @PostMapping("/updateDriverHome")
    @Operation(summary = "更新司机家")
    public Result updateDriverHome(@RequestBody @Valid UpdateDriverHomeForm form) {
        int rows = driverService.updateDriverHome(form);
        return Result.ok().put("rows", rows);
    }

}